package runnerworm.core;

import java.awt.*;

/**
 * <p>Title: WormParticle</p>
 * <p>Description: A part of a worm is called a worm particle - cool isn't it?</p>
 * @author Guilherme Mauro Germoglio Barbosa
 */

public class WormParticle {

  /**
   * Its x coordinate.
   */
  private double x;
  /**
   * Its y coordinate.
   */
  private double y;
  /**
   * The particle width.
   */
  private final int PARTICLE_WIDTH = 3;
  /**
   * The particle height.
   */
  private final int PARTICLE_HEIGHT = 3;




  /**
   * Constructor. Initializes the coordinates.
   * @param x The initial x.
   * @param y the initial y.
   */
  public WormParticle(double x, double y) {
    this.x = x;
    this.y = y;
  }

  /**
   * Returns the x coordinate.
   * @return The x coordinate.
   */
  public double get_X() {
    return x;
  }

  /**
   * Returns the y coordinate.
   * @return The y coordinate.
   */
  public double get_Y() {
    return y;
  }

  /**
   * Sets the x coordinate.
   * @param new_X The new X coordinate.
   */
  public void set_X(double new_X) {
    this.x = new_X;
  }

  /**
   * Sets the y coordinate.
   * @param new_Y The new Y coordinate.
   */
  public void set_Y(double new_Y) {
    this.y = new_Y;
  }

  /**
   * Draws the worm particle.
   * @param g Where the particle will be drawn.
   */
  public synchronized void draw(Graphics g) {
    g.fillOval((int)x, (int)y, PARTICLE_WIDTH, PARTICLE_HEIGHT);
  }

  /**
   * Checks if the polygon given intersects the particle.
   * @param polygon The polygon.
   * @return True if intersects, false case not.
   */
  public boolean intersects(Polygon polygon) {
    return PolygonIntersectionChecker.checkIntersection(getPolygon(), polygon);
  }

  /**
   * Creates a polygon that represents this worm particle.
   * @return A polygon that represents this worm particle.
   */
  public Polygon getPolygon() {
    Polygon thisWormParticle = new Polygon();
    int direction = 0;
    for (int j = 0; j < 10; j++) {
      thisWormParticle.addPoint(
          (int) (x +
                 PARTICLE_HEIGHT *
                 Math.cos(direction + j * 2 * Math.PI / 10)),
          (int) (y +
                 PARTICLE_WIDTH *
                 Math.sin(direction + j * 2 * Math.PI / 10))
          );
    }
    return thisWormParticle;
  }

}